package cn.iocoder.yudao.module.engine.service.core.queue.audio.impl;

import cn.iocoder.yudao.module.engine.service.core.queue.audio.AudioQueue;
import cn.iocoder.yudao.module.engine.model.engine.EngineOutputData;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

@Slf4j
public class NotLimitedAudioQueue implements AudioQueue {

    private final LinkedList<EngineOutputData.Audio> queue;
    private final ReentrantLock lock;

    public NotLimitedAudioQueue() {
        queue = new LinkedList<>();
        lock = new ReentrantLock();
    }

    @Override
    public boolean addAudio(EngineOutputData.Audio audio) {
        lock.lock();
        try {
            // No business restrictions, just add to the end of queue
            queue.add(audio);
            log.info("[NotLimited] Added audio to queue, chatId = {}", audio.getChatId());
            return true;
        } finally {
            lock.unlock();
        }
    }

    @Override
    public EngineOutputData.Audio removeAudio() {
        lock.lock();
        try {
            if (queue.isEmpty()) {
                return null;
            }
            return queue.removeFirst();
        } finally {
            lock.unlock();
        }
    }

    @Override
    public void clear() {
        lock.lock();
        try {
            queue.clear();
        } finally {
            lock.unlock();
        }
    }

    @Override
    public List<EngineOutputData.Audio> getDataList() {
        lock.lock();
        try {
            return new ArrayList<>(queue);
        } finally {
            lock.unlock();
        }
    }

}
